import { useRef, useState } from 'react';

const useQuestionsData = () => {
  // V: 视觉
  // A：听觉
  // R： 读写
  // K：运动
  const data = useRef({
    V: 0,
    A: 0,
    R: 0,
    K: 0,
  });
  const changeData = (value) => {
    switch (value) {
      case 'V':
        data.current.V++;
        break;
      case 'A':
        data.current.A++;
        break;
      case 'R':
        data.current.R++;
        break;
      case 'K':
        data.current.K++;
        break;
      default:
        return;
    }
  };
  const changeDataSelect = (value) => {
    switch (value) {
      case 'V':
        data.current.V--;
        break;
      case 'A':
        data.current.A--;
        break;
      case 'R':
        data.current.R--;
        break;
      case 'K':
        data.current.K--;
        break;
      default:
        return;
    }
  };

  // 保存当前题号 从0 开始 故要+1
  const [step, setStep] = useState(0);
  const jumpToNextQuestion = (value) => {
    if (step < 16) {
      changeData(value);
    }
    // 延迟跳转
    setTimeout(() => {
      if (step === 18) {
        console.log('当前结果集合为：', data);
        return;
      }
      setStep(step + 1);
    }, 1000);
  };

  const jumpToLastQuestion = () => {
    setStep((setStep) => setStep - 1);
  };

  // 处理返回上一题后，用户改变选择
  const changeSelect = (value, preValue) => {
    changeDataSelect(preValue);
    changeData(value);
    // 延迟跳转
    setTimeout(() => {
      if (step === 18) {
        return;
      }
      setStep(step + 1);
    }, 1000);
  };
  return { step, data, jumpToNextQuestion, jumpToLastQuestion, changeSelect };
};

export default useQuestionsData;
